
package org.upt.dcda.db;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * The persistent class for the users database table.
 * 
 */
@Entity
@Table(name = "users")
@NamedNativeQueries(value =
{
// @NamedNativeQuery(name = UserEntity.CREATE_USER_QUERY, query =
// "INSERT INTO Users(user_first_name, user_last_name, user_email, user_password, user_address) VALUES (:firstName, :lastName, :email, SHA1(:password), :address)"),
// @NamedNativeQuery(name = UserEntity.VERIFY_CREDENTIALS_QUERY, query =
// "SELECT COUNT(1) FROM Users WHERE user_email = :email AND user_password = SHA1(:password)",
// resultClass = Integer.class),
// @NamedNativeQuery(name = UserEntity.MODIFY_CREDENTIALS_QUERY, query =
// "UPDATE Users SET user_password = SHA1(:newPassword) WHERE user_id = :id AND user_password = SHA1(:currentPassword)")
// })
})
@NamedQueries(value =
{ @NamedQuery(name = UserEntity.VERIFY_CREDENTIALS_QUERY, query = "SELECT ue FROM UserEntity ue WHERE ue.userEmail = :email AND ue.userPassword = :password")
// @NamedQuery(name = UserEntity.CREATE_USER_QUERY, query = "INSERT ") })
})
public class UserEntity implements Serializable
{

	public static final String		CREATE_USER_QUERY			= "create_user_query";

	public static final String		VERIFY_CREDENTIALS_QUERY	= "verify_credentials_query";

	public static final String		MODIFY_CREDENTIALS_QUERY	= "modify_credentials_query";

	private static final long		serialVersionUID			= 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "user_id")
	private int						userId;

	@Column(name = "user_address")
	private String					userAddress;

	@Column(name = "user_email")
	private String					userEmail;

	@Column(name = "user_first_name")
	private String					userFirstName;

	@Column(name = "user_last_name")
	private String					userLastName;

	@Column(name = "user_password")
	private String					userPassword;

	@OneToMany(mappedBy = "user")
	private Set<NotificationEntity>	notifications;

	@OneToMany(mappedBy = "user")
	private Set<OrderEntity>		orders;

	@OneToMany(mappedBy = "user")
	private Set<BookReviewEntity>	bookreviews;

	public UserEntity()
	{
	}

	public UserEntity(String firstName, String lastName, String email, String password, String address)
	{
		setUserFirstName(firstName);
		setUserLastName(lastName);
		setUserEmail(email);
		setUserPassword(password);
		setUserAddress(address);
	}

	public int getUserId()
	{
		return this.userId;
	}

	public String getUserAddress()
	{
		return this.userAddress;
	}

	public void setUserAddress(String userAddress)
	{
		this.userAddress = userAddress;
	}

	public String getUserEmail()
	{
		return this.userEmail;
	}

	public void setUserEmail(String userEmail)
	{
		this.userEmail = userEmail;
	}

	public String getUserFirstName()
	{
		return this.userFirstName;
	}

	public void setUserFirstName(String userFirstName)
	{
		this.userFirstName = userFirstName;
	}

	public String getUserLastName()
	{
		return this.userLastName;
	}

	public void setUserLastName(String userLastName)
	{
		this.userLastName = userLastName;
	}

	private String getUserPassword()
	{
		return this.userPassword;
	}

	private void setUserPassword(String userPassword)
	{
		this.userPassword = userPassword;
	}

	// bi-directional many-to-one association to NotificationEntity
	public Set<NotificationEntity> getNotifications()
	{
		return this.notifications;
	}

	public void setNotifications(Set<NotificationEntity> notifications)
	{
		this.notifications = notifications;
	}

	// bi-directional many-to-one association to OrderEntity
	public Set<OrderEntity> getOrders()
	{
		return this.orders;
	}

	public void setOrders(Set<OrderEntity> orders)
	{
		this.orders = orders;
	}

	// bi-directional many-to-one association to BookReviewEntity
	public Set<BookReviewEntity> getBookreviews()
	{
		return this.bookreviews;
	}

	public void setBookreviews(Set<BookReviewEntity> bookreviews)
	{
		this.bookreviews = bookreviews;
	}

	public boolean isAdmin()
	{
		return "admin@admin.admin".equals(this.userEmail);
	}
}
